// pages/selfRemind/setremind/setremind.js
const app = getApp();
var dateTimePicker = require('../../utils/dateTimePicker.js');
var util = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    selfRemind: {
      selfRemindId: null,
      selfRemindContent: "",
      selfRemindDate: null,
      /* selfRemindTime: null, */
      selfRemindDetail: "",
    },
    /* //时间和日期
    time: null,
    date: null, */
    //判断是修改还是增加
    newOrModi: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.newOrModi == '1') 
    {
      var queryBean = JSON.parse(options.queryBean);
      this.setData({
        selfRemind: queryBean,
        newOrModi: 1
      })
    } 
    /* else 
    {
      var time1 = dateTimePicker.getHourMinu();
      var date1 = util.formatDate1(new Date)
      var stime = "selfRemind.selfRemindTime"
      var sDate = "selfRemind.selfRemindDate"
      this.setData({
        [stime]: time1,
        time: time1,
        [sDate]: date1,
        date: date1
      });
    } */
  },


  /**
   * 输入备忘录标题
   */
  bindKeyInput1: function (e) {
    var scontent = "selfRemind.selfRemindContent"
    this.setData({
      [scontent]: e.detail.value
    })
  },

  /**
   * 输入备忘录内容
   */
  bindKeyInput3: function (e) {
    var sdetail = "selfRemind.selfRemindDetail"
    this.setData({
      [sdetail]: e.detail.value
    })
  },

  /**
   * 时间选择
   */
  /* bindTimeChange: function (e) {
    var stime = "selfRemind.selfRemindTime"
    this.setData({
      [stime]: e.detail.value
    })
  }, */

  /**
   * 日期选择
   */
  /* bindDateChange: function (e) {
    var sdate = "selfRemind.selfRemindDate"
    this.setData({
      [sdate]: e.detail.value
    })
  }, */

  /**
   * 添加/修改完成
   */
  setRemind: function (e) {
    var url = getApp().globalData.url;
    if(this.data.newOrModi == 0)
    {
      if (this.data.selfRemind.selfRemindContent == '' || this.data.selfRemind.selfRemindContent == undefined) 
      {
        wx.showToast({
          title: '请输入备忘录名称',
          icon: 'none',
          duration: 2000
        })
        return;
      } 
      else
      {
        wx.request({
          url: url + 'addSelfRemind',
          method: 'POST',
          data: {
            assistantTel: getApp().globalData.username,
            content: this.data.selfRemind.selfRemindDetail,
            title: this.data.selfRemind.selfRemindContent,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success(res) {
            if (res.data == '添加成功') {
              wx.showToast({
                title: '保存成功',
                icon: 'none',
                duration: 2000
              })
            }
            else if (res.data == false && res.data == "false" && res.data == "") {
              wx.showToast({
                title: '保存失败',
                icon: 'none',
                duration: 2000
              })
            }
          },
          fail(res) {
            wx.showToast({
              title: '网络连接失败',
              icon: 'none',
              duration: 2000
            })
          },
        })
      }
    }
    if(this.data.newOrModi == 1)
    {
      wx.request({
        url: url + 'modifySelfRemind',
        method: 'POST',
        data: {
          remindId: this.data.selfRemind.selfRemindId,
          remindDetail: this.data.selfRemind.selfRemindDetail,
          remindContent: this.data.selfRemind.selfRemindContent,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success(res) {
          if (res.data == '更新成功') {
            wx.showToast({
              title: '更新成功',
              icon: 'none',
              duration: 2000
            })
          }
          else if (res.data == false && res.data == "false" && res.data=="") {
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail(res) {
          wx.showToast({
            title: '网络连接失败',
            icon: 'none',
            duration: 2000
          })
        },
      })
    }
  }
})